package com.itheima.project.face.impl;

import com.itheima.project.constant.dict.PlacesCacheConstant;
import com.itheima.project.core.enums.dict.PlacesEnum;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.face.PlacesFace;
import com.itheima.project.pojo.Places;
import com.itheima.project.service.IPlacesService;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.ExceptionsUtil;
import com.itheima.project.vo.dict.PlacesVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName PlacesFaceImpl.java
 * @Description 省市县face服务实现
 */
@Slf4j
@Component
public class PlacesFaceImpl implements PlacesFace {

    @Autowired
    IPlacesService placesService;

    @Override
    @Cacheable(value = PlacesCacheConstant.LIST,key = "#parentId")
    public List<PlacesVo> findPlacesVoListByParentId(Long parentId) throws ProjectException {
        try {
            List<Places> places = placesService.findPlacesVoListByParentId(parentId);
            return BeanConv.toBeanList(places,PlacesVo.class);
        }catch (Exception e){
            log.error("查询parentId下列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(PlacesEnum.LIST_FAIL);
        }
    }
}
